package indi.kider.server.woniubus.api;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.model.apabus.LogOpt;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.woniubus.LogOptQuery;
import indi.kider.server.woniubus.service.LogOptService;
import indi.kider.server.woniubus.service.related.AuthcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;

import java.util.List;
import java.util.Map;

/**
 * 操作日志控制器
 */
@RestController
@RequestMapping(value = "logOpt")
public class LogOptController {

    @Autowired
    private LogOptService logOptService;
    @Autowired
    private AuthcService authcService;

    /**
     * @api {POST} /insert
     * http://localhost:9002/woniubus/logOpt/insert
     */
    @PutMapping("/insert")
    public CommonResponse insertLogOpt(@RequestBody LogOpt logOpt) {
        try {
            Employee employee = authcService.getCurrentUser().getData();
            logOpt.setEmpId(employee.getId());
            return logOptService.insertLogOpt(logOpt);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {POST} /queryLogOptList
     * @apiGroup wnbus
     * @apiDescription 操作日志（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/logOpt/queryLogOptList
     */
    @PostMapping("/queryLogOptList")
    public CommonResponse<PageResponse<List<Map>>> queryLogOptList(@RequestBody PageRequest<LogOptQuery> request) {
        Employee employee = authcService.getCurrentUser().getData();
        if (employee != null && !employee.getRoles().equals(EmployeeRoleCons.ADMIN)) {//超级管理员
            request.getQuery().setEnterpriseId(employee.getEnterpriseId());
        }
        return CommonResponse.buildGetSuccessResponse(PageUtil.getPageResponse(logOptService.queryLogOptList(request)));
    }

}
